package org.elephas.webapp.model;

import org.apache.wicket.injection.web.InjectorHolder;
import org.apache.wicket.model.LoadableDetachableModel;
import org.apache.wicket.spring.injection.annot.SpringBean;
import org.elephas.model.Comment;
import org.elephas.service.CommentFinder;

public class DetachableCommentModel extends LoadableDetachableModel<Comment> {
	private static final long serialVersionUID = 1L;

	@SpringBean
	private CommentFinder service;

	private String commentId = null;

	public DetachableCommentModel() {
		InjectorHolder.getInjector().inject(this);
	}

	public DetachableCommentModel(Comment comment) {
		this();
		if (comment != null) {
			commentId = comment.getUuid();
		}
	}

	public DetachableCommentModel(String commentId) {
		this();
		this.commentId = commentId;
	}

	@Override
	protected Comment load() {
		if (commentId == null) {
			return new Comment();
		}
		return service.findById(commentId);
	}
}
